﻿using System.Collections.Generic;

namespace SimulChess
{
    public class ChessPiece
    {
        public PieceColor Color;
        public PieceName Name;
        public int PositionX;
        public int PositionY;

        protected int RelativeValue;
        protected Movement[,] MoveMatrix;
        protected bool HasMoved;
        protected bool CanJump;
        protected bool Upgradeable;

        public ChessPiece()
        {
            this.Initialize();
        }

        public ChessPiece(PieceName Piece, PieceColor PlayerColor)
        {
            this.Initialize();

            this.Name = Piece;
            this.Color = PlayerColor;

            SetMoveMatrix();
        }

        public void MovePiece(ChessBoard Board, int X, int Y)
        {
            Board.Position[this.PositionX, this.PositionY].Occupied = false;

            this.PositionX = X;
            this.PositionY = Y;

            this.HasMoved = true;

            Board.Position[this.PositionX, this.PositionY].Occupied = true;
        }

        public virtual bool[,] ShowMoves(ChessBoard Board)
        {
            bool[,] moveArray = new bool[8, 8];

            for (int y = 0; y < 15; y++)
            {
                for (int x = 0; x < 15; x++)
                {
                    int relativeX = this.PositionX + (x-7);
                    int relativeY = this.PositionY + (y-7);

                    if ((relativeX < 0) || (relativeY < 0) || (relativeX > 7) || (relativeY > 7))
                        continue;

                    moveArray[relativeX, relativeY] = false;

                    if (((Board.Position[relativeX, relativeY].Occupied == false)
                            || ((this.MoveMatrix[x, y] == Movement.allowed) || (this.MoveMatrix[x, y] == Movement.capture) || (this.MoveMatrix[x, y] == Movement.both)))
                        && ((this.MoveMatrix[x, y] != Movement.blocked)
                            || ((this.HasMoved == false) && (this.MoveMatrix[x, y] == Movement.allowedOnce))))
                    {
                        moveArray[relativeX, relativeY] = true;
                    }                    
                }
            }

            return moveArray;
        }

        protected virtual void SetMoveMatrix()
        {
            return;
        }

        private void Initialize()
        {
            this.RelativeValue = 0;
            this.HasMoved = false;
            this.CanJump = false;
            this.Upgradeable = false;

            this.MoveMatrix = new Movement[15, 15];
            ClearMoveMatrix();
        }

        private void ClearMoveMatrix()
        {
            for (int y = 0; y < 15; y++)
                for (int x = 0; x < 15; x++)
                    this.MoveMatrix[x, y] = Movement.blocked;

            this.MoveMatrix[7, 7] = Movement.allowed;

            return;
        }

    }
}